package org.wedding.center.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import org.wedding.center.dto.query.InventoryQuery;
import org.wedding.center.entity.InventoryInfo;
import org.wedding.center.mapper.InventoryInfoMapper;
import org.wedding.center.service.InventoryInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.wedding.enums.system.ResultCodeEnum;
import org.wedding.utils.CommonResult;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import org.wedding.dto.PageModel;
import org.wedding.utils.PageUtil;

import java.util.List;
/**
 * 服务库存表(InventoryInfo)表服务实现类
 *
 * @author songkaiyue
 * @since 2025-05-19 17:26:20
 */
@Service
public class InventoryInfoServiceImpl extends ServiceImpl<InventoryInfoMapper, InventoryInfo> implements InventoryInfoService {
    /**
     * 分页查询所有数据
     * @return
     */
    @Override
    public CommonResult<PageModel> findAll(InventoryQuery query) {
        try {
            List<InventoryInfo> list = baseMapper.selectAll(query);
            List<InventoryInfo> setList = query.setList(PageUtil.getListPage(query.getCurrent(), query.getPageSize(), list), list.size());
            query.setList(setList, list.size());
            return CommonResult.success(query);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }

    }

    /**
     * 根据id查看详情
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult<InventoryInfo> view(String id) {
        try {
            InventoryInfo inventoryInfo = baseMapper.findById(id);
            return CommonResult.success(inventoryInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 新增数据
     *
     * @param inventoryInfo
     * @return
     */
    @Override
    public CommonResult add(InventoryInfo inventoryInfo) {
        try {
            inventoryInfo.setId(IdUtil.simpleUUID());
            inventoryInfo.setCreateTime(DateUtil.now());
            inventoryInfo.setDelFlag("0");
            String userId = StpUtil.getSession().get("userId").toString();
            String userName = StpUtil.getSession().get("userName").toString();
            inventoryInfo.setUserId(userId);
            inventoryInfo.setUserName(userName);
            baseMapper.insert(inventoryInfo);
            return CommonResult.success(inventoryInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 修改数据
     *
     * @param inventoryInfo
     * @return
     */
    @Override
    public CommonResult updateEntity(InventoryInfo inventoryInfo) {
        try {
            inventoryInfo.setUpdateTime(DateUtil.now());
            baseMapper.updateById(inventoryInfo);
            return CommonResult.success(inventoryInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

      /**
     * 删除数据
     *
     * @param ids
     * @return
     */
    @Override
    public CommonResult delete(List<String> ids) {
        try {
            if (ObjectUtil.isEmpty(ids)) return CommonResult.failed(ResultCodeEnum.PARAM_ERROR);
            for (String id : ids) {
                baseMapper.upDeleteById(id);
            }
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }
    
}
